define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'vue'], function ($, undefined, Backend, Table, Form, Vue) {
    return {
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'vv/goods/index' + location.search,
                    add_url  : '',
                    edit_url : 'vv/goods/edit',
                    del_url  : '',
                    multi_url: '',
                    table    : 'wanlshop_goods'
                }
            });
            const table = $("#table")
            table.bootstrapTable({
                url     : $.fn.bootstrapTable.defaults.extend.index_url,
                pk      : 'id',
                sortName: 'weigh',
                columns : [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {field: 'shop_id', title: 'shop_id', visible: false},
                        {field: 'shop.shopname', title: '店铺名'},
                        {field: 'image', title: '商品主图', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'images', title: '产品相册', events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'category_id', title: '商品类目', operate: 'IN'},
                        {field: 'category.name', title: '商品类目', formatter: Table.api.formatter.search},
                        {field: 'shopsort.name', title: '店铺类目', formatter: Table.api.formatter.search},
                        {field: 'stock', title: '库存计算方式', searchList: {"porder": '下单减库存', "payment": '付款减库存'}, formatter: Table.api.formatter.normal},
                        {field: 'price', title: '产品价格', operate: 'BETWEEN'},
                        // {field: 'freight_id', title: __('Freight_id')},
                        // {field: 'grounding', title: __('Grounding')},
                        {field: 'specs', title: '商品规格', searchList: {"single": '单规格', "multi": '多规格'}, formatter: Table.api.formatter.normal},
                        // {field: 'distribution', title: __('Distribution'), searchList: {"true":__('Distribution true'),"false":__('Distribution false')}, formatter: Table.api.formatter.normal},
                        // {field: 'activity', title: __('Activity'), searchList: {"true":__('Activity true'),"false":__('Activity false')}, formatter: Table.api.formatter.normal},
                        {field: 'views', title: '点击'},
                        {field: 'sales', title: '销量'},
                        {field: 'comment', title: '评论'},
                        {field: 'praise', title: '好评'},
                        {field: 'like', title: '收藏'},
                        {field: 'createtime', title: '创建时间', operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: '更新时间', operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: '状态', searchList: {"normal": __('Normal'), "hidden": __('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            })
            Table.api.bindevent(table)
        },
        edit : function () {
            var vm = new Vue({
                el: '#app',
                data() {
                    return {
                        spu          : Config.spu,
                        spuItem      : Config.spuItem,
                        sku          : Config.sku,
                        skuItem      : Config.skuItem,
                        categoryId   : Config.categoryId,
                        categoryList : Config.channelList,
                        categoryOne  : null,
                        categoryTwo  : null,
                        categoryThree: null,
                        categoryFour : null,
                        categoryFive : null,
                        attribute    : Config.attribute,
                        attributeData: [],
                        batch        : 0
                    }
                },
                mounted() {
                    this.categoryList.forEach((item, index) => {
                        if (item.id == Config.categoryId) {
                            this.categoryOne = index;
                        } else {
                            item.childlist.forEach((item1, index1) => {
                                if (item1.id == Config.categoryId) {
                                    this.categoryOne = index;
                                    this.categoryTwo = index1;
                                } else {
                                    item1.childlist.forEach((item2, index2) => {
                                        if (item2.id == Config.categoryId) {
                                            this.categoryOne   = index;
                                            this.categoryTwo   = index1;
                                            this.categoryThree = index2;
                                        } else {
                                            item2.childlist.forEach((item3, index3) => {
                                                if (item3.id == Config.categoryId) {
                                                    this.categoryOne   = index;
                                                    this.categoryTwo   = index1;
                                                    this.categoryThree = index2;
                                                    this.categoryFour  = index3;
                                                } else {
                                                    item3.childlist.forEach((item4, index4) => {
                                                        if (item4.id == Config.categoryId) {
                                                            this.categoryOne   = index
                                                            this.categoryTwo   = index1
                                                            this.categoryThree = index2
                                                            this.categoryFour  = index3
                                                            this.categoryFive  = index4
                                                        }
                                                    })
                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        }
                    })
                    Fast.api.ajax("vv/goods/attribute?id=" + this.categoryId, data => {
                            this.attributeData = data
                            return false
                        }
                    )
                },
                methods: {
                    // 页面上传图片
                    changeImage(event, key) {
                    },
                    getCategory(e) {
                        Fast.api.ajax("wanlshop.goods/attribute?id=" + this.categoryId, (data, ret) => {
                            this.attributeData = data
                            return false;
                        });
                    },
                    spuAdd() {
                        var str = this.$refs['specs-name'].value || ''
                        str     = str.trim();
                        if (!str) {
                            layer.msg("产品属性不能为空");
                            return
                        }
                        // 遍历
                        var arr = str.split(/\s+/);
                        for (var i = 0; i < arr.length; i++) {
                            this.spu.push(arr[i])
                        }
                        // 清空表单
                        this.$refs['specs-name'].value = ''
                    },
                    // 添加规格
                    skuAdd(index) {
                        console.log(this.spuItem);
                        var str = this.$refs['specs-name-' + index][0].value || ''
                        str     = str.trim();
                        if (!str) {
                            layer.msg("产品属性不能为空")
                            return
                        }
                        // 遍历
                        var arr = str.split(/\s+/);
                        for (var i = 0; i < arr.length; i++) {
                            if (this.spuItem[index]) {
                                this.spuItem[index].push(arr[i])
                            } else {
                                this.spuItem.push([arr[i]])
                            }
                        }
                        // 清空表单
                        this.$refs['specs-name-' + index][0].value = ""
                        this.skuCreate();
                    },
                    // 删除属性
                    spuRemove(key) {
                        Vue.delete(vm.spuItem, key);
                        Vue.delete(vm.spu, key);
                        this.skuCreate();
                    },
                    // 删除规格
                    skuRemove(i, key) {
                        Vue.delete(vm.spuItem[i], key);
                        this.skuCreate();
                    },
                    // 生成Sku
                    skuCreate() {
                        this.sku = this.skuDesign(this.spuItem)
                    },
                    skuDesign(array) {
                        if (array.length == 0) return []
                        if (array.length < 2) {
                            var res = []
                            array[0].forEach(function (v) {
                                res.push([v])
                            })
                            return res
                        }
                        return [].reduce.call(array, function (col, set) {
                            var res = [];
                            col.forEach(function (c) {
                                set.forEach(function (s) {
                                    var t = [].concat(Array.isArray(c) ? c : [c]);
                                    t.push(s);
                                    res.push(t);
                                })
                            });
                            return res;
                        });
                    },
                    skuBatch() {
                        this.batch = this.batch == 0 ? 1 : 0;
                    },
                    cdnurl(url) {
                        if (url) return Fast.api.cdnurl(url);
                    }
                }
            })
            Form.api.bindevent($("form[role=form]"))
            $('button').prop('disabled', true)
            $('a').css('pointer-events', 'none').attr('disabled', true)
            setTimeout(() => {
                $('#c-content').summernote('disable')
            }, 444)
        }
    }
})